package com.hfut.eeg.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.hfut.eeg.common.api.CommonPage;
import com.hfut.eeg.common.api.CommonResult;
import com.hfut.eeg.common.util.EEGUtil;
import com.hfut.eeg.common.util.GenerateCommon;
import com.hfut.eeg.dto.PatientListResult;
import com.hfut.eeg.dto.PatientSingleParam;
import com.hfut.eeg.dto.QueryPatientsParam;
import com.hfut.eeg.service.PatientInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.controller
 * @Description: TODO
 * @date 2019-11-05 18:47
 */
@Controller
@Api(tags = "PatientListController", value = "病人列表")
@RequestMapping("/home")
public class PatientListController {

    @Autowired
    private PatientInfoService patientInfoService;

    @Autowired
    private GenerateCommon common;


    @ApiOperation("获取病人列表")
    @RequestMapping(value = "/patientList")
    @ResponseBody
    public CommonResult patientList(@RequestParam(value = "pageSize", defaultValue = "11") Integer pageSize,
                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        //System.out.println("pageSize:"+pageSize+"; pageNum:"+pageNum);
        List<PatientListResult> results = patientInfoService.getPatientList(pageSize,pageNum);
        if(results==null){
            return CommonResult.failed("获取列表失败");
        }else {
            return CommonResult.success(CommonPage.restPage(results));
        }
    }

    @ApiOperation("获取病人列表")
    @RequestMapping(value = "/queryPatient")
    @ResponseBody
    public CommonResult queryPatient(QueryPatientsParam param,
                                    @RequestParam(value = "pageSize", defaultValue = "11") Integer pageSize,
                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        List<PatientListResult> results = patientInfoService.queryPatientsList(param,pageSize,pageNum);
        if(results==null){
            return CommonResult.failed("获取列表失败");
        }else {
            return CommonResult.success(CommonPage.restPage(results));
        }
    }

    @ApiOperation("根据Id删除病人信息")
    @RequestMapping(value = "/deletePatient", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deletePatient(@RequestParam(value = "patientId") String id,
                                      @RequestParam(value = "checkTime")String checkTime){
        //System.out.println(checkTime);
        int result = patientInfoService.deletePatientById(id,checkTime);
        if(result==0){
            return CommonResult.failed("删除病人信息失败");
        }else {
            return CommonResult.success("删除成功");
        }
    }


    @ApiOperation("添加病人信息")
    @RequestMapping(value = "/addPatient", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addPatient(@RequestBody PatientSingleParam param){
        //利用规则生成id键
        common.initID(patientInfoService);
        param.setPatientId(common.generateID(patientInfoService));
        param.setCheckStatus(0);
        int offset = 0-param.getOld();
        param.setBirthDate(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR,offset).toSqlDate());

        int result = patientInfoService.addPatient(param);
        //System.out.println(result);
        HashMap<String,Object> map = new HashMap<>();
        map.put("Id",param.getPatientId());
        boolean tag = EEGUtil.checkEEGForPatient(param.getName());
        if(tag){
            Thread thread = new Thread(() -> common.stimulateEEG(param.getPatientId(),1,3));
            thread.start();
        }
        map.put("checkStatus", tag);
        if(result == 0){
            return CommonResult.failed("添加病人失败");
        }else {
            return CommonResult.success(map);
        }
    }

    @ApiOperation("获取当日待就诊病人信息")
    @RequestMapping(value = "/getDateDiagnosePatient")
    @ResponseBody
    public CommonResult getDatePatient(){
        HashMap<String,Object> result = patientInfoService.getDiagnoseDatePatient();
        if(result == null){
            return CommonResult.failed("获取信息失败");
        }else {
            return CommonResult.success(result);
        }
    }

    @ApiOperation("修改病人检查状态信息")
    @RequestMapping(value = "/changePatientStatus")
    @ResponseBody
    public CommonResult changePatientStatus(String PatientId){
        int res = patientInfoService.changePatientStatus(PatientId);
        if(res <= 0){
            return CommonResult.failed("修改病人状态信息失败");
        }else {
            return CommonResult.success("修改病人状态成功");
        }
    }

    @ApiOperation("根据病人Id获取病人信息")
    @RequestMapping(value = "/getPatientById")
    @ResponseBody
    public CommonResult getPatientById(String PatientId){
        if(PatientId==null ||"".equals(PatientId.trim())){
            return CommonResult.failed("请传入正确的参数："+PatientId);
        }
        PatientListResult result = patientInfoService.getPatientById(PatientId);
        if(result == null){
            return CommonResult.failed("获取病人信息失败");
        }else {
            return CommonResult.success(result);
        }
    }
}
